package com.cdream.basic.algorithm.leetcode.array;

import java.util.Arrays;

/**
 * @author cdream
 * @date 12/3/21
 */
public class Question1005 {
    public int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (k == 0 || nums[i] >= 0) {
                break;
            } else {
                nums[i] = -nums[i];
            }
        }
        Arrays.sort(nums);
        if (k % 2 == 0) {
            return Arrays.stream(nums).sum();
        } else {
            nums[0] = -nums[0];
            return Arrays.stream(nums).sum();
        }

    }

    public static void main(String[] args) {
        new Question1005().findJudge(2,new int[][]{{1,2}});
    }
    public int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] top = new int[grid.length];
        int[] left = new int[grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            left[i] = Arrays.stream(grid[i]).max().orElse(0);
        }
        for (int j = 0; j < grid[0].length; j++) {
            int max = 0;
            for (int i = 0; i < grid.length; i++) {
                if (grid[i][j] > max) {
                    max = grid[i][j];
                }
            }
            top[j] = max;
        }
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                int min = Math.min(left[i],top[j]);
                if (min>grid[i][j]){
                    sum +=min- grid[i][j];
                }
            }
        }
        return sum;

    }

    public int findJudge(int n, int[][] trust) {
        int[] res = new int[n];
        for(int[] t : trust){
            res[t[1]-1]++;
        }
        int k = -1;
        for(int i =0;i<res.length ; i++){
            if(res[i]==n-1){
                if(k== -1){
                    k = i;
                }else {
                    return -1;
                }
            }
        }
        for(int[] t : trust){
            if(t[0]==k){
                return -1;
            }
        }
        return k+1;
    }
}
